#include "game.h"


Clock::Clock()
{
	frameTimeTicker = 0;
}

bool Clock::ticker()
{
	frameTimeTicker++;

	g_theTimer.mark();

	if (frameTimeTicker >= g_theTimer.mdFrameTime*3000)
	{
		frameTimeTicker = 0;

		return true;
	}
	else
	{
		return false;
	}
}

bool Clock::ticker(double time)
{
	frameTimeTicker++;

	time *= 3000;

	g_theTimer.mark();

	if (frameTimeTicker >= g_theTimer.mdFrameTime*time)
	{
		frameTimeTicker = 0;

		return true;
	}
	else
	{
		return false;
	}
}

bool Game::getGameActive()
{
	return gameIsActive;
}

void Game::setGameActive(bool newCondition)
{
	gameIsActive = newCondition;
}

void RectangularGameObject::setPosition(Vector2D newPosition)
{
	m_position = newPosition;
}

Vector2D RectangularGameObject::getPosition()
{
	return m_position;
}

Vector2D RectangularGameObject::getVelocity()
{
	return m_velocity;
}

float RectangularGameObject::getHeight()
{
	return height;
}

float RectangularGameObject::getWidth()
{
	return width;
}

void CircularGameObject::setPosition(Vector2D newPosition)
{
	m_position = newPosition;
}

Vector2D CircularGameObject::getPosition()
{
	return m_position;
}

Vector2D CircularGameObject::getVelocity()
{
	return m_velocity;
}

float CircularGameObject::getRadius()
{
	return radius;
}

void RectangularGameObject::init(Vector2D thePosition, float theHeight, float theWidth)
{
	theShape.PlaceAt(thePosition.YValue+(theHeight/2),thePosition.XValue+(theWidth/2),
		thePosition.YValue-(theHeight/2),thePosition.XValue-(theWidth/2));

	m_position = thePosition;
	height = theHeight;
	width = theWidth;
}

void RectangularGameObject::Draw()
{
	MyDrawEngine::GetInstance()->FillRect(theShape, MyDrawEngine::BLUE);
}

void RectangularGameObject::Draw(int colour)
{
	MyDrawEngine::GetInstance()->FillRect(theShape, colour);
}

void RectangularGameObject::Update(Vector2D theVelocity)
{
	m_velocity = theVelocity;
	m_position += m_velocity;

	theShape.PlaceAt(m_position.YValue+(height/2),m_position.XValue+(width/2),
		m_position.YValue-(height/2),m_position.XValue-(width/2));
}

bool RectangularGameObject::CollidesWith(RectangularGameObject& other)
{
	Rectangle2D tempShape;
	Rectangle2D secondShape;

	tempShape.PlaceAt(this->m_position.YValue+height,this->m_position.XValue-width,
		this->m_position.YValue-height, this->m_position.XValue+width);

	secondShape.PlaceAt(other.m_position.YValue+height,other.m_position.XValue-width,
		other.m_position.YValue-height,	other.m_position.XValue+width);

	if (tempShape.Intersects(secondShape))
	{
		return true;
	}
	else
	{
		return false;
	}
}

void CircularGameObject::init(Vector2D thePosition, float theRadius)
{
	m_position = thePosition;
	radius = theRadius;
}

void CircularGameObject::Draw()
{
	MyDrawEngine::GetInstance()->FillCircle(m_position, radius, MyDrawEngine::BLUE);
}

void CircularGameObject::Draw(int colour)
{
	MyDrawEngine::GetInstance()->FillCircle(m_position, radius, colour);
}

void CircularGameObject::Update(Vector2D theVelocity)
{
	m_velocity = theVelocity;
	m_position += m_velocity;
}

bool CircularGameObject::CollidesWith(CircularGameObject& other)
{
	Circle2D tempShape;
	Circle2D secondShape;

	tempShape.PlaceAt(this->m_position,this->radius);
	secondShape.PlaceAt(other.m_position,other.radius);

	if(tempShape.Intersects(secondShape))
	{
		return true;
	}
	else 
	{
		return false;
	}
}

bool CircularGameObject::CollidesWith(RectangularGameObject& other)
{
	Circle2D tempShape;
	Rectangle2D secondShape;

	float secondHeight = other.getHeight()/2;
	float secondWidth = other.getWidth()/2;

	tempShape.PlaceAt(this->m_position,this->radius);
	secondShape.PlaceAt(other.getPosition().YValue+secondHeight,
		other.getPosition().XValue-secondWidth,
		other.getPosition().YValue-secondHeight,
		other.getPosition().XValue+secondWidth);

	if (tempShape.Intersects(secondShape))
	{
		return true;
	}
	else
	{
		return false;
	}
}

void Image::init(Vector2D thePosition, Vector2D theSize, char* _filename)
{
	m_position = thePosition;
	m_size = theSize;

	theImage = MyDrawEngine::GetInstance()->LoadPicture(_filename);

	if (theImage != 0)
	{
		hasImage = true;
	}
	else
	{
		hasImage = false;
	}

	collisionBox.init(thePosition,theSize.YValue,theSize.XValue);
}

void Image::Draw()
{
	if(hasImage)
	{
		MyDrawEngine::GetInstance()->DrawAt(m_position, theImage
			, m_size);
	}
	else
	{
		MyDrawEngine::GetInstance()->WriteText((int)m_position.XValue, (int)m_position.YValue,
			"No image loaded", MyDrawEngine::GREEN,0);
	}
	
}

void Image::Update(Vector2D theVelocity)
{
	collisionBox.Update(theVelocity);

	m_position += theVelocity;
}

bool Image::CollidesWith(Image& other)
{
	Rectangle2D tempShape;
	Rectangle2D secondShape;

	float secondHeight = other.collisionBox.getHeight()/2;
	float secondWidth = other.collisionBox.getWidth()/2;

	tempShape.PlaceAt(this->collisionBox.getPosition().YValue+this->collisionBox.getHeight(),
		this->collisionBox.getPosition().XValue-this->collisionBox.getWidth(),
		this->collisionBox.getPosition().YValue-this->collisionBox.getHeight(), 
		this->m_position.XValue+this->collisionBox.getWidth());

	secondShape.PlaceAt(other.m_position.YValue+other.collisionBox.getHeight(),
		other.m_position.XValue-other.collisionBox.getWidth(),
		other.m_position.YValue-other.collisionBox.getHeight(),	
		other.m_position.XValue+other.collisionBox.getWidth());

	if (tempShape.Intersects(secondShape))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Image::CollidesWith(RectangularGameObject& other)
{
	Rectangle2D tempShape;
	Rectangle2D secondShape;

	tempShape.PlaceAt(this->m_position.YValue+this->collisionBox.getHeight()
		,this->m_position.XValue-this->collisionBox.getWidth(),
		this->m_position.YValue-this->collisionBox.getHeight()
		, this->m_position.XValue+this->collisionBox.getWidth());

	secondShape.PlaceAt(other.getPosition().YValue+other.getHeight(),
		other.getPosition().XValue-other.getWidth(),
		other.getPosition().YValue-other.getHeight(),	
		other.getPosition().XValue+other.getWidth());

	if (tempShape.Intersects(secondShape))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Image::CollidesWith(CircularGameObject& other)
{
	Circle2D tempShape;
	Rectangle2D secondShape;

	tempShape.PlaceAt(other.getPosition(),other.getRadius());
	secondShape.PlaceAt(this->m_position.YValue+this->collisionBox.getHeight()
		,this->m_position.XValue-this->collisionBox.getWidth(),
		this->m_position.YValue-this->collisionBox.getHeight()
		, this->m_position.XValue+this->collisionBox.getWidth());

	if (tempShape.Intersects(secondShape))
	{
		return true;
	}
	else
	{
		return false;
	}
}

void Image::setPosition(Vector2D newPosition)
{
	m_position = newPosition;
}

Vector2D Image::getPosition()
{
	return m_position;
}

Vector2D Image::getVelocity()
{
	return m_velocity;
}

Vector2D Image::getSize()
{
	return m_size;
}

GameResult Game::GetResult()
{
	return theResult;
}

void TextWriter(Vector2D textPosition, const char DisplayText[], int colour)
{
	MyDrawEngine::GetInstance()->WriteText((int)textPosition.XValue, (int)textPosition.YValue, DisplayText, colour, 0);
}

void IntWriter(Vector2D numberPosition, const int DisplayInt, int colour)
{
	MyDrawEngine::GetInstance()->WriteInt((int)numberPosition.XValue, (int)numberPosition.YValue,
		DisplayInt, colour, 0);
}

void NumberWriter(Vector2D numberPosition, const double DisplayNum, int colour)
{
	MyDrawEngine::GetInstance()->WriteDouble((int)numberPosition.XValue, (int)numberPosition.YValue,
		DisplayNum, colour, 0);
}